package com.sweets.look.ui.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sweets.look.R;
import com.sweets.look.util.ViewUtils;

public class MarkMultiChoiceDialog {
    private Context mContext;
    private Dialog mDialog;
    private TextView mDialogMsg;
    private TextView mDialogTitle;
    private Display mDisplay;
    private LinearLayout mLayoutContent;
    private TextView mNegativeBtn;
    private TextView mNeutralBtn;
    private TextView mPositiveBtn;
    private String mResult;
    private ScrollView mScrollMsg;

    public MarkMultiChoiceDialog(Context context) {
        super();
        this.mContext = context;
        this.mDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    }

    public MarkMultiChoiceDialog(Context context, String title, String msg) {
        super();
        this.mContext = context;
        this.mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        this.builder();
        if(title != null) {
            this.setTitle(title);
        }

        if(msg != null) {
            this.setMessage(msg);
        }

        this.setPositiveButton(this.mContext.getResources().getString(android.R.string.ok), null);//17039370
        this.show();
    }

    public MarkMultiChoiceDialog builder() {
        View v2 = LayoutInflater.from(this.mContext).inflate(R.layout.widget_multi_choice_dialog,
                (ViewGroup) ((Activity) this.mContext).findViewById(android.R.id.content), false);//16908290
        View v0 = v2.findViewById(R.id.dialog_layout);
        this.mDialogTitle = (TextView) v2.findViewById(R.id.dialog_title);
        this.mDialogTitle.setVisibility(View.GONE);
        this.mScrollMsg = (ScrollView) v2.findViewById(R.id.msg_scroll);
        this.mScrollMsg.setVisibility(View.GONE);
        this.mDialogMsg = (TextView) v2.findViewById(R.id.dialog_msg);
        this.mDialogMsg.setVisibility(View.GONE);
        this.mPositiveBtn = (TextView) v2.findViewById(R.id.dialog_btn_positive);
        this.mPositiveBtn.setVisibility(View.GONE);
        this.mNegativeBtn = (TextView) v2.findViewById(R.id.dialog_btn_negative);
        this.mNegativeBtn.setVisibility(View.GONE);
        this.mNeutralBtn = (TextView) v2.findViewById(R.id.dialog_btn_neutral);
        this.mNeutralBtn.setVisibility(View.GONE);
        this.mLayoutContent = (LinearLayout) v2.findViewById(R.id.lLayout_content);
        this.mDialog = new Dialog(this.mContext, R.style.DialogTheme);
        this.mDialog.setContentView(v2);
        ((RelativeLayout)v0).setLayoutParams(new FrameLayout.LayoutParams(Math.min(this.mDisplay.getWidth(),
                ViewUtils.convertToDensityPixels(this.mContext, 300)), -2));
        this.mDialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
        return this;
    }

    public void dismiss() {
        if(this.mDialog != null && (this.mDialog.isShowing())) {
            this.mDialog.dismiss();
        }
    }

    public MarkMultiChoiceDialog setCancelable(boolean cancel) {
        this.mDialog.setCancelable(cancel);
        return this;
    }

    public MarkMultiChoiceDialog setCanceledOnTouchOutside(boolean cancel) {
        this.mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public MarkMultiChoiceDialog setMessage(String msg) {
        this.mDialogMsg.setVisibility(View.VISIBLE);
        this.mDialogMsg.setText(((CharSequence)msg));
        this.mScrollMsg.setVisibility(View.VISIBLE);
        int v1 = ViewUtils.getWindowHeight(this.mContext) - ViewUtils.convertToDensityPixels(this.mContext,
                200);
        if(ViewUtils.measureTextViewHeight(this.mContext, msg, 16) >= v1) {
            ViewGroup.LayoutParams v0 = this.mScrollMsg.getLayoutParams();
            v0.height = v1;
            this.mScrollMsg.setLayoutParams(v0);
        }

        return this;
    }

    public MarkMultiChoiceDialog setMultiChoiceItems(String[] items, boolean[] checked, String posbtn,
                                                     final DialogInterface.OnClickListener listener) {
        this.mResult = "2";
        int v6 = 0;
        int v7 = 0;
        this.mScrollMsg.setVisibility(View.VISIBLE);
        int v3 = 0;
        int v9 = items.length - 1;
        while(v3 <= v9) {
            View v1 = LayoutInflater.from(this.mContext).inflate(R.layout.ui_item_checkbox,
                    (ViewGroup) ((Activity)this.mContext).findViewById(android.R.id.content), false);//16908290
            ((CheckBox)v1).setText(items[v3]);
            if(v3 > checked.length - 1) {
                ((CheckBox)v1).setChecked(false);
            }
            else {
                ((CheckBox)v1).setChecked(checked[v3]);
            }

            this.mLayoutContent.addView(v1);
            if(v6 == 0) {
                ((CheckBox)v1).measure(View.MeasureSpec.makeMeasureSpec(ViewUtils.getWindowWidth(this
                        .mContext), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                v6 = ((CheckBox)v1).getMeasuredHeight();
            }

            v7 += v6;
            ++v3;
        }

        int v8 = ViewUtils.getWindowHeight(this.mContext) - ViewUtils.convertToDensityPixels(this.mContext,
                200);
        if(v7 >= v8) {
            ViewGroup.LayoutParams v5 = this.mScrollMsg.getLayoutParams();
            v5.height = v8;
            this.mScrollMsg.setLayoutParams(v5);
        }

        this.mPositiveBtn.setVisibility(View.VISIBLE);
        this.mPositiveBtn.setText(posbtn);
        this.mPositiveBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int v2;
                for(v2 = 0; v2 < MarkMultiChoiceDialog.this.mLayoutContent.getChildCount(); ++v2) {
                    View v3 = MarkMultiChoiceDialog.this.mLayoutContent.getChildAt(v2);
                    if(v3.getVisibility() == View.VISIBLE && v3.getId() != R.id.dialog_msg) {
                        MarkMultiChoiceDialog.this.mResult = ((CheckBox)v3).isChecked() ? MarkMultiChoiceDialog.
                                this.mResult + "1" : MarkMultiChoiceDialog.this.mResult + "0";
                    }
                }

                if(listener != null) {
                    listener.onClick(null, Integer.parseInt(MarkMultiChoiceDialog.this.mResult));
                }

                MarkMultiChoiceDialog.this.mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkMultiChoiceDialog setNegativeButton(String text, final View.OnClickListener listener) {
        this.mNegativeBtn.setVisibility(View.VISIBLE);
        this.mNegativeBtn.setText(((CharSequence)text));
        this.mNegativeBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(listener != null) {
                    listener.onClick(v);
                }

                MarkMultiChoiceDialog.this.mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkMultiChoiceDialog setNeutralButton(String text, final View.OnClickListener listener) {
        this.mNeutralBtn.setVisibility(View.VISIBLE);
        this.mNeutralBtn.setText(((CharSequence)text));
        this.mNeutralBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(listener != null) {
                    listener.onClick(v);
                }

                MarkMultiChoiceDialog.this.mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkMultiChoiceDialog setPositiveButton(String text, final View.OnClickListener listener) {
        this.mPositiveBtn.setVisibility(0);
        this.mPositiveBtn.setText(((CharSequence)text));
        this.mPositiveBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(listener != null) {
                    listener.onClick(v);
                }

                MarkMultiChoiceDialog.this.mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkMultiChoiceDialog setTitle(String title) {
        this.mDialogTitle.setVisibility(View.VISIBLE);
        this.mDialogTitle.setText(((CharSequence)title));
        return this;
    }

    public void show() {
        this.mDialog.show();
    }
}

